objCt = document.getElementById("dndupload_ct");
uploadUrl = document.getElementById('upload_url').value;
function dragenterHandler(e){
	this.setAttribute("style","background:none repeat scroll 0 0 #555555;height:100%;left:0;min-width:800px;opacity:0.4; overflow:visible;position:absolute;text-align:center;top:0;width:100%;z-index: 9999;");
}
function dragoverHandler(e){
	e.stopPropagation();
	e.preventDefault();
}
function dropHandler(e){
	e.stopPropagation();
	e.preventDefault();
	var files = e.dataTransfer.files; 
	var fr = new FileReader();
	
	var xhr = null;
	if (window.XMLHttpRequest){
		xhr=new XMLHttpRequest();
	}else if (window.ActiveXObject){
		xhr=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	for (var i = 0, f; f = files[i]; i++) {
		tmp_file = f;
		ext = getExt(tmp_file.name);
		//alert(tmp_file.type);
		fr.onloadend = function(){
			if( xhr ){
				var url = uploadUrl+"?oldname="+tmp_file.name+"&ext="+ext;
				xhr.open("POST",url,true);
				xhr.overrideMimeType("application/octet-stream");
				if(!XMLHttpRequest.prototype.sendAsBinary){
				  XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
					function byteValue(x) {
					  return x.charCodeAt(0) & 0xff;
					}
					var ords = Array.prototype.map.call(datastr, byteValue);
					var ui8a = new Uint8Array(ords);
					this.send(ui8a.buffer);
				  }
				}
				
				xhr.sendAsBinary(fr.result);
				
				xhr.onreadystatechange = function(){
					//alert(xhr.state);
					if( xhr.readyState==4 && xhr.status==200 ){
						successCallback(xhr.responseText);
						// var reg = /^image/;
						// if( reg.exec(tmp_file.type) ){
						// 	var html = '<p><a href="'+xhr.responseText+'" title="'+tmp_file.name+'" target="_blank">'+tmp_file.name+'</a></p>';
						// 	document.getElementById("file_list").innerHTML += html;
						// }else{
						// 	document.getElementById("file_list").innerHTML += '<p>'+xhr.responseText+'</p>';
						//}
					}
				}				
			}
		}
		fr.readAsBinaryString(f);
	}	
	
	this.setAttribute("style","");
}

function getExt(file){
	var ext;
	if( file=="" ){
		ext = "unknown";
	}
	var arr = file.split('.');
	var str = arr[arr.length-1];
	if( str=="" || typeof(str)=="undefined" ){
		ext = "unknown";
	}else{
		ext = str;
	}
	return ext;
}
if( window.FileReader ){
	$("#dnd_upload_list").before('<p>您也可以将图片文件拖放到本窗口上传。</p>');
	objCt.addEventListener('dragenter',dragenterHandler,false);
	objCt.addEventListener('dragover',dragoverHandler,false);
	objCt.addEventListener('drop',dropHandler,false);
}